import {
	mapActions,
	mapGetters
} from 'vuex';
import {
	fakeDeleteLibrary,
	updateLibraryName
} from '@/api/data/tbNetDiskDir';
const getStrLength = str => {
	let char = str.replace(/[^\x00-\xff]/g, '**');
	return char.length;
}
export default {
	components: {},
	props: {
		data: {
			type: Array,
			default () {
				return [];
			}
		}
	},
	data() {
		return {
			defaultCover: '',
			defaultAvatar: ''
		}
	},
	computed: {
		...mapGetters(['siteInfo', 'userInfo'])
	},
	mounted() {
		this.getDefaultCover();
		this.getDefaultAvatar();
	},
	methods: {
		...mapActions(['getFileUrl']),

		//下拉菜单指令定义
		commandObject(type, data) {
			return {
				type,
				data
			}
		},

		//获取默认封面
		async getDefaultCover() {
			let result = await this.getFileUrl(this.siteInfo.field3);
			this.defaultCover = window.staticPath + result;
		},

		//获取默认用户头像
		async getDefaultAvatar() {
			let result = await this.getFileUrl(this.siteInfo.userPic);
			this.defaultAvatar = window.staticPath + result;
		},

		//库封面
		libraryCover(data) {
			let imageUrl = data.coverInfo?.attrAddr;
			if (!imageUrl) {
				return this.defaultCover;
			} else {
				return window.staticPath + imageUrl;
			}
		},

		//头像图片
		avatarImage(data) {
			let avatarUrl = data.creator?.attrAddr;
			if (!avatarUrl) {
				return this.defaultAvatar;
			} else {
				return avatarUrl;
			}
		},

		//库指令操作
		handleLibraryCommand(data) {
			switch (data.type) {
				case 'edit':
					this.handleEditLibrary(data.data);
					break;
				case 'rename':
					this.handleRenameLibrary(data.data);
					break;
				case 'download':
					this.handleDownloadLibrary(data.data);
					break;
				case 'delete':
					this.handleDeleteLibrary(data.data);
					break;
				case 'detail':
					this.handleLookLibInfo(data.data)
					break;
			}
		},

		//编辑库
		handleEditLibrary(data) {
			this.$emit('edit', data);
		},

		//重命名库
		handleRenameLibrary(data) {
			let that = this;
			that.$prompt('请输入字符不超过25的汉字、数字、字母或下划线', '重命名', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				inputValue: data.name,
				inputValidator(value) {
					let regexp = /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/;
					if (!value) {
						return '请输入库名称';
					} else if (getStrLength(value) >= 25) {
						return '输入的库名称超过25字符';
					} else if (!regexp.test(value)) {
						return '库名称输入字符出现错误';
					}
				},
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						let params = {
							id: data.id,
							name: instance.inputValue
						}
						instance.confirmButtonLoading = true;
						await updateLibraryName(params).then(res => {
							let result = res.data;
							if (result.code === '0') {
								that.$message.success('重命名库成功')
							} else {
								that.$message.error('重命名库失败')
							}
						});
						instance.confirmButtonLoading = false;
						that.$emit('refresh');
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//下载库
		handleDownloadLibrary(data) {
			this.$alert('下载功能开发中', '提示', {
				confirmButtonText: '确定',
				callback: action => {}
			});
		},

		//删除库
		handleDeleteLibrary(data) {
			let that = this;
			that.$confirm(`此操作将删除资源库<font color="#f00">【${data.name}】</font>, 是否继续?`, '删除提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				dangerouslyUseHTMLString: true,
				type: 'warning',
				async beforeClose(action, instance, done) {
					instance.confirmButtonLoading = true;
					await fakeDeleteLibrary(data.id).then(res => {
						let result = res.data;
						if (result.code === '0') {
							that.$message.success('删除库成功');
						} else {
							that.$message.error('删除库失败');
						}
					})
					instance.confirmButtonLoading = false;
					that.$emit('refresh');
					done();
				}
			}).catch(() => {});
		},

		//查看资源库信息
		handleLookLibInfo(data) {
			this.$emit('look', data)
		},

		//进入库资源页面
		handleLookLibrary(data) {
			this.$router.push({
				path: '/data/resourcelib/' + data.id,
				query: {
					name: data.name
				}
			});
		},
	}
}